var base64 = require("../weui/example/images/base64");
//获取应用实例
const app = getApp()

Page({
  data: {
    userId: null,
    noticeArr: [],
    knowledgeArr: [],
    noticePageNo: 1,
    noticePageSize: 20,
    klPageNo: 1,
    klPageSize: 20,
    displayType: 0,
    lookMoreNotice: '数据加载中...',
    wh:0
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  selectKl: function (e) {
    this.setData({
      displayType: 0
    });
  },
  selectNotice: function (e) {
    this.setData({
      displayType: 1
    });
  },
  toNoticeDetail: function (e) {
    wx.navigateTo({
      url: '../noticeDetail/noticeDetail?id=' + e.currentTarget.dataset.contentid + '&userId=' + this.data.userId
    })
  },
  toDeleteNotice: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/like/deleteByRecordReturnData',
      data: {
        pageNo: 1,
        pageSize: this.data.noticePageSize,
        userId: this.data.userId,
        contentId: e.currentTarget.dataset.contentid,
        contentType: 1
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            noticePageNo: 1
          });
          if (res.data.recordList.length == that.data.pageSize) {
            that.data.lookMoreNotice = '-上滑查看更多-'
          }
          else {
            that.data.lookMoreNotice = '-没有更多内容-'
          }
          that.data.noticeArr = res.data.recordList
          that.setData({
            noticeArr: that.data.noticeArr,
            lookMoreNotice: that.data.lookMoreNotice
          })
        }
        else {
          that.setData({
            noticeArr: [],
            lookMoreNotice: '-暂无收藏-'
          })
        }
      }
    })
  },
  toDeleteAllNotice: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/like/deleteAllByUserId',
      data: {
        pageNo: 1,
        pageSize: this.data.noticePageSize,
        userId: this.data.userId,
        contentType: 1
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            noticePageNo: 1
          });
          if (res.data.recordList.length == that.data.pageSize) {
            that.data.lookMoreNotice = '-上滑查看更多-'
          }
          else {
            that.data.lookMoreNotice = '-没有更多内容-'
          }
          that.data.noticeArr = res.data.recordList
          that.setData({
            noticeArr: that.data.noticeArr,
            lookMoreNotice: that.data.lookMoreNotice
          })
        }
        else {
          that.setData({
            noticeArr: [],
            lookMoreNotice: '-暂无收藏-'
          })
        }
      }
    })
  },
  toKnowledgeDetail: function (e) {
    wx.navigateTo({
      url: '../klDetail/klDetail?id=' + e.currentTarget.dataset.contentid + '&userId=' + this.data.userId
    })
  },
  toDeleteKnowledge: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/like/deleteByRecordReturnData',
      data: {
        pageNo: 1,
        pageSize: this.data.klPageSize,
        userId: this.data.userId,
        contentId: e.currentTarget.dataset.contentid,
        contentType: 0
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            klPageNo: 1
          });
          if (res.data.recordList.length == that.data.pageSize) {
            that.data.lookMoreKnowledge = '-上滑查看更多-'
          }
          else {
            that.data.lookMoreKnowledge = '-没有更多内容-'
          }
          that.data.knowledgeArr = res.data.recordList
          that.setData({
            knowledgeArr: that.data.knowledgeArr,
            lookMoreKnowledge: that.data.lookMoreKnowledge
          })
        }
        else {
          that.setData({
            knowledgeArr: [],
            lookMoreKnowledge: '-暂无收藏-'
          })
        }
      }
    })
  },
  toDeleteAllKnowledge: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/like/deleteAllByUserId',
      data: {
        pageNo: 1,
        pageSize: this.data.klPageSize,
        userId: this.data.userId,
        contentType: 0
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            klPageNo: 1
          });
          if (res.data.recordList.length == that.data.pageSize) {
            that.data.lookMoreKnowledge = '-上滑查看更多-'
          }
          else {
            that.data.lookMoreKnowledge = '-没有更多内容-'
          }
          that.data.knowledgeArr = res.data.recordList
          that.setData({
            knowledgeArr: that.data.knowledgeArr,
            lookMoreKnowledge: that.data.lookMoreKnowledge
          })
        }
        else {
          that.setData({
            knowledgeArr: [],
            lookMoreKnowledge: '-暂无收藏-'
          })
        }
      }
    })
  },
  onShow: function (options) {
    if (app.globalData.userId) {
      var that = this;
      this.setData({
        userId: app.globalData.userId,
        noticePageNo: 1,
        klPageNo: 1
      })
      wx.request({
        url: 'https://www.xinhuo.site/api/like/list',
        data: {
          pageNo: this.data.noticePageNo,
          pageSize: this.data.noticePageSize,
          contentType: 1,
          userId: this.data.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            if (res.data.recordList.length == that.data.pageSize) {
              that.data.lookMoreNotice = '-上滑查看更多-'
            }
            else {
              that.data.lookMoreNotice = '-没有更多内容-'
            }
            that.data.noticeArr = res.data.recordList
            that.setData({
              noticeArr: that.data.noticeArr,
              lookMoreNotice: that.data.lookMoreNotice
            })
          }
          else {
            that.setData({
              lookMoreNotice: '-暂无收藏-'
            })
          }
        }
      })
      wx.request({
        url: 'https://www.xinhuo.site/api/like/list',
        data: {
          pageNo: this.data.klPageNo,
          pageSize: this.data.klPageSize,
          contentType: 0,
          userId: this.data.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            if (res.data.recordList.length == that.data.pageSize) {
              that.data.lookMoreKnowledge = '-上滑查看更多-'
            }
            else {
              that.data.lookMoreKnowledge = '-没有更多内容-'
            }
            that.data.knowledgeArr = res.data.recordList
            that.setData({
              knowledgeArr: that.data.knowledgeArr,
              lookMoreKnowledge: that.data.lookMoreKnowledge
            })
          }
          else {
            that.setData({
              lookMoreKnowledge: '-暂无收藏-'
            })
          }
        }
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  loadMore() {
    var that = this;
    if (this.data.displayType == 1){
      this.setData({
        noticePageNo: this.data.noticePageNo + 1
      });
      wx.request({
        url: 'https://www.xinhuo.site/api/like/list',
        data: {
          pageNo: this.data.noticePageNo,
          pageSize: this.data.noticePageSize,
          contentType: 1,
          userId: this.data.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            if (res.data.recordList.length == that.data.noticePageSize) {
              that.data.lookMoreNotice = '-上滑查看更多-'
            }
            else {
              that.data.lookMoreNotice = '-没有更多内容-'
            }
            that.data.noticeArr = that.data.noticeArr.concat(res.data.recordList)
            that.setData({
              noticeArr: that.data.noticeArr,
              lookMoreNotice: that.data.lookMoreNotice
            })
          }
          else {
            that.setData({
              lookMoreNotice: '-没有更多内容-'
            })
          }
        }
      })
    }
    else {
      this.setData({
        klPageNo: this.data.klPageNo + 1
      });
      wx.request({
        url: 'https://www.xinhuo.site/api/like/list',
        data: {
          pageNo: this.data.klPageNo,
          pageSize: this.data.klPageSize,
          contentType: 0,
          userId: this.data.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            if (res.data.recordList.length == that.data.klPageSize) {
              that.data.lookMoreKnowledge = '-上滑查看更多-'
            }
            else {
              that.data.lookMoreKnowledge = '-没有更多内容-'
            }
            that.data.knowledgeArr = that.data.knowledgeArr.concat(res.data.recordList)
            that.setData({
              knowledgeArr: that.data.knowledgeArr,
              lookMoreKnowledge: that.data.lookMoreKnowledge
            })
          }
          else {
            that.setData({
              lookMoreKnowledge: '-没有更多内容-'
            })
          }
        }
      })
    }
  }
})
